const WXAPI = require('../../wxapi/main')
const con = require("../../utils/data.js");
const regeneratorRuntime = require('../../utils/runtime')
    //获取应用实例
var app = getApp()
Page({
    data: {
        showRegionStr: '请选择',
      region: ['江西省', '南昌市', '青山湖区'],
    },
  RegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
    bindSave: function(e) {
        // 保存到自己的后台
        var that = this;
        let  regions = e.detail.value.regions[0] + ',' + e.detail.value.regions[1] + ',' + e.detail.value.regions[2];
        var linkMan = e.detail.value.linkMan;
        var address = e.detail.value.address;
        var mobile = e.detail.value.mobile;
        var code = e.detail.value.code;
        var params = {
            openid: wx.getStorageSync('OpenId'),
            regions: regions,
            linkMan: linkMan,
            addr: address,
            mobile: mobile,
            code: code,
            isDefault: 1
        }
        console.log(params, 140)

        // ---------------
        WXAPI.addTempleMsgFormid({
            token: wx.getStorageSync('token'),
            type: 'form',
            formId: e.detail.formId
        })
        var that = this;
        var linkMan = e.detail.value.linkMan;
        var address = e.detail.value.address;
        var mobile = e.detail.value.mobile;
        var code = e.detail.value.code;
        if (linkMan == "") {
            wx.showModal({
                title: '提示',
                content: '请填写联系人姓名',
                showCancel: false
            })
            return
        }
        if (mobile == "") {
            wx.showModal({
                title: '提示',
                content: '请填写手机号码',
                showCancel: false
            })
            return
        }
      if (!regions) {
            wx.showModal({
                title: '提示',
                content: '请选择地区',
                showCancel: false
            })
            return
        }
        if (address == "") {
            wx.showModal({
                title: '提示',
                content: '请填写详细地址',
                showCancel: false
            })
            return
        }
        if (code == "") {
            wx.showModal({
                title: '提示',
                content: '请填写邮编',
                showCancel: false
            })
            return
        }
        wx.request({
        url: con.index_saveaddress,
        // url: 'http://192.168.0.107:8080/wechat/saveAddress',
        method: 'GET',
        data: params,
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(res.data);
          console.log('返回成功');
          wx.showToast({
            title: '保存成功',
            icon:'none'
          })
          setTimeout(() => {
            wx.navigateBack({})
          }, 1000);
          
        },
        fail: function (res) {
          console.log('返回失败');
        }
      })
    },
    onLoad: function(e) {
      var that = this;
      wx.request({
        url: 'https://www.brvpmusic.com/wechat/addrDetils',
          // url: 'http://192.168.0.107:8080/wechat/addrDetils',
        method: 'GET',
        data: {
          openid: wx.getStorageSync('OpenId'),
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(res.data);
          console.log('返回成功');
          that.setData({
            wxaddress:res.data.body
          });
        },
        fail: function (res) {
          console.log('返回失败');
        }
      })
    },
    deleteAddress: function(e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        wx.showModal({
            title: '提示',
            content: '确定要删除该收货地址吗？',
            success: function(res) {
                if (res.confirm) {
                    WXAPI.deleteAddress(id, wx.getStorageSync('token')).then(function() {
                        wx.navigateBack({})
                    })
                } else {
                    console.log('用户点击取消')
                }
            }
        })
    }
})